package org.dromara.business.api;


import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.AppUser;
import org.dromara.business.domain.vo.SelfUserVO;
import org.dromara.business.request.BindParentUserRequest;
import org.dromara.business.request.BindUserRequest;
import org.dromara.business.service.AppUserService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.AppLoginUser;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.vo.SysUserVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user")
public class AppUserApi {

    private final AppUserService appUserService;

    /**
     * 用户绑定钱包地址
     * @param request
     * @return
     */
    @PostMapping("/bind")
    public R<Void> bindUser(@RequestBody @Validated BindUserRequest request) {
        appUserService.bindUser(request);
        return R.ok();
    }

    /**
     * 绑定上级钱包地址
     * @param request
     * @return
     */
    @PostMapping("/bind/parent")
    public R<Void> bindParentUser(@RequestBody @Validated BindParentUserRequest request) {
        appUserService.bindParentUser(request);
        return R.ok();
    }

    @GetMapping("/self")
    public R<SelfUserVO> getSelf() {
        AppLoginUser loginUser = LoginHelper.getAppLoginUser();
        AppUser user = appUserService.getById(loginUser.getUserId());
        if (ObjectUtil.isNull(user)) {
            return R.fail("没有权限访问用户数据!");
        }
        SelfUserVO selfUserVO = MapstructUtils.convert(user, SelfUserVO.class);
        return R.ok(selfUserVO);
    }

}
